<?php
namespace Tongcheng\Controller;
use Think\Controller;
class AdvController extends CommonController {
    public function index(){
    	$model = M("Adv");
    	$map['wid'] = session('wid');
    	$list = $model->where($map)->select();
        $this->assign('list',$list);

        $adv = array("index"=>"首页","shop"=>'好店',"house"=>'房产页',"job"=>'招聘页',"pinche"=>'拼车页');
        $this->assign('adv',$adv);
        $this->display();
    }

    public function add(){
        if(IS_AJAX){
            $model = M("Adv");
            $new['wid'] = session('wid');
            $new['type'] = I('post.type');
            $new['imgurl'] = I('post.imgurl');
            $new['create_time'] = date("Y-m-d H:i:s");
            if($model->add($new)){
                $this->success('添加成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $this->display();
        }
    }

    public function edit(){
    	$model = M("Adv");
    	$map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        if(IS_AJAX){
            $new['type'] = I('post.type');
            $new['imgurl'] = I('post.imgurl');
            $model->where($map)->save($new);
            // echo $model->getLastSql();
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();
            $this->assign('vo',$vo);
            $this->display('add');
        }
    }

    public function del(){
        $model = M("Adv");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

    //
    public function homeFlash ()
    {
        $model = M("Adv");
        $map['wid'] = session('wid');
        $map['type'] = 'home';
        $list = $model->where($map)->select();
        $this->assign('list',$list);

        $adv = array("home"=>"首页轮播");
        $this->assign('adv',$adv);
        $this->display();
    }

    //
    public function addHomeFlash(){
        if(IS_AJAX){
            $model = M("Adv");
            $new['wid'] = session('wid');
            $new['type'] = I('post.type');
            $new['imgurl'] = I('post.imgurl');
            $new['create_time'] = date("Y-m-d H:i:s");
            if($model->add($new)){
                $this->success('添加成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $this->display();
        }
    }

    //
    public function editHomeFlash ()
    {
        $model = M("Adv");
        $map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        if(IS_AJAX){
            $new['type'] = I('post.type');
            $new['imgurl'] = I('post.imgurl');
            $model->where($map)->save($new);
            // echo $model->getLastSql();
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();
            $this->assign('vo',$vo);
            $this->display('addHomeFlash');
        }
    }

    //
    public function shopAdv() {
        $where['a.wid'] = session('wid');
        $where['a.type'] = 1;
        $list = M('adv_shop')->alias('a')
                             ->field('a.id, a.type, a.status, b.name, b.logo_url, b.cate')
                             ->where($where)
                             ->join('tp_shop b on a.shop_id = b.shop_id')
                             ->order('a.status desc')
                             ->select();
        $this->assign('list', $list);
        $this->display();
    }

    //
    public function addShopAdv () {
        if ($_POST['name']) {
            $whereOr['name'] = ['like', '%'. $_POST['name']. '%'];
            $whereOr['cate'] = ['like', '%'. $_POST['name']. '%'];
            $whereOr['_logic'] = 'or';
            $where['_complex'] = $whereOr;
        }
        $where['wid'] = session('wid');
        $count = M('shop')->where($where)->count();
        $Page = new \Think\Page($count,9);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();
        $list = M('shop')->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('show', $show);
        $this->display();
    }
    
    //
    public function doAddShopAdv () {
        $save['wid'] = session('wid');
        $save['shop_id'] = $_POST['id'];
        $save['type'] = 1;
        $save['status'] = 1;
        $save['createTime'] = date('Y-m-d H:i:s', time());
        $row = M('adv_shop')->add($save);
        if ($row) {
            $this->success("添加成功！");
        } else {
            $this->error("网络忙，请稍后再试！");
        }
    }

    //
    public function delShopAdv () {
        $model = M("adv_shop");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];
        if($model->where($map)->delete()){
            $this->success("删除成功！");
        } else {
            $this->error("网络忙，请稍后再试！");
        }
    }

    //
    public function editShopAdv () {
        $model = M("adv_shop");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];
        $arr = $model->where($map)->find();
        if ($arr['status']) {
            $save['status'] = 0;
        } else {
            $save['status'] = 1;
        }
        $row = $model->where($map)->save($save);
        if ($row) {
            $this->success("修改成功！");
        } else {
            $this->error("网络忙，请稍后再试！");
        }
    }
}